package cn.npe.dynamic.thread.pool.sdk.registry;

import cn.npe.dynamic.thread.pool.sdk.domain.model.entity.DtpThreadPoolExecutor;
import cn.npe.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.npe.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolProperties;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

public class DtpContext {
    /**
     * 储存线程池
     */
    private static final Map<String, ThreadPoolExecutor> EXECUTOR_MAP = new ConcurrentHashMap<>();

    /**
     * 获取线程池
     * @param executorName 线程池名字
     */
    public static ThreadPoolExecutor getExecutor(String executorName) {
        return EXECUTOR_MAP.get(executorName);
    }


    public static Collection<String> getAllExecutorNames(){
        return EXECUTOR_MAP.keySet();
    }


    public static Collection<ThreadPoolExecutor> getAllDtpExecutor(){
        return EXECUTOR_MAP.values();
    }
    /**
     * 线程池注册
     * @param executorName 线程池名字
     */
    public static void add(String executorName, ThreadPoolExecutor executor) {
        //注册
        EXECUTOR_MAP.put(executorName, executor);
    }


    /**
     * 刷新线程池参数
     * @param threadPoolConfigEntity 线程池名字
     *
     */
    public static void refresh(ThreadPoolConfigEntity threadPoolConfigEntity) {
        DtpThreadPoolExecutor executor = (DtpThreadPoolExecutor) EXECUTOR_MAP.get(threadPoolConfigEntity.getThreadPoolName());
        if(executor!=null) {
            //刷新参数
            executor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
            executor.setMaximumPoolSize(threadPoolConfigEntity.getMaximumPoolSize());
            //executor.setRejectedExecutionHandler(executor.getHandler(threadPoolConfigEntity.re));
        }
    }
}